package com.sskj.gcc.presenter.game;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.MD5Util;
import com.sskj.gcc.bean.game.GameActiveBean;
import com.sskj.gcc.bean.game.GameCommunitiesListBean;
import com.sskj.gcc.bean.game.GameExchangeParametersBean;
import com.sskj.gcc.bean.game.GameGetFoodBean;
import com.sskj.gcc.bean.game.GameIndexBean;
import com.sskj.gcc.bean.game.GameNotificationBean;
import com.sskj.gcc.bean.game.GamePackgaeBean;
import com.sskj.gcc.presenter.BasePresenter;
import com.sskj.gcc.ui.fragment.GameBaseFragment;
import com.sskj.gocoin.MyAppLication;
import com.sskj.gocoin.m.HttpConfig;
import com.sskj.gocoin.m.bean.UserInfo;
import com.sskj.lib.http.JsonCallBack;

import java.util.List;

public class GameFragmentPresenter extends BasePresenter<GameBaseFragment> {

    /**
     * 获取个人信息
     */
    public void requestUserInfo() {
        OkGo.<HttpData<UserInfo>>get(HttpConfig.BASE_URL + HttpConfig.GET_USER_INFO)
                .params("mobile", MyAppLication.getMobile())
                .execute(new JsonCallBack<HttpData<UserInfo>>() {
                    @Override
                    public void onSuccess(Response<HttpData<UserInfo>> response) {
                        HttpData<UserInfo> httpData = response.body();
                        if (httpData.getCode() == 200) {
                            mView.requestUserInfoSuccess(httpData.getData());
                        }
                    }

                    @Override
                    public void onError(Response<HttpData<UserInfo>> response) {
                        super.onError(response);
                        HttpData httpListData = response.body();
                        if (httpListData != null) {
                            mView.onError(httpListData.getMsg());
                        }
                    }
                });
    }

    /**
     * 首页游戏 index
     */
    public void requestGameIndex() {
        OkGo.<HttpData<GameIndexBean>>get(HttpConfig.BASE_URL + HttpConfig.GAME_INDEX)
                .execute(new JsonCallBack<HttpData<GameIndexBean>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<GameIndexBean>> response) {
                        HttpData<GameIndexBean> httpListData = response.body();
                        if (httpListData.getCode() == 200) {
                            mView.onGameIndexSuccess(httpListData.getData());
                        } else {
                            mView.onError(httpListData.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<HttpData<GameIndexBean>> response) {
                        super.onError(response);
                        HttpData<GameIndexBean> httpListData = response.body();
                        if (httpListData != null) {
                            mView.onError(httpListData.getMsg());
                        }
                    }
                });
    }

    /**
     * 喂养食料
     */
    public void eatfeedoff() {
        OkGo.<HttpData<String>>get(HttpConfig.BASE_URL + HttpConfig.GAME_EATFEEDOFF)
                .params("mobile", MyAppLication.getMobile())
                .execute(new JsonCallBack<HttpData<String>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<String>> response) {
                        HttpData httpListData = response.body();
                        if (httpListData.getCode() == 200) {
                            mView.onEatFeedOffSuccess(httpListData.getMsg());
                        } else {
                            mView.onError(httpListData.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<HttpData<String>> response) {
                        super.onError(response);
                        HttpData httpListData = response.body();
                        if (httpListData != null) {
                            mView.onError(httpListData.getMsg());
                        }
                    }
                });
    }

    /**
     * 首页游戏 动态列表
     */
    public void getRedynamic() {
        OkGo.<HttpData<GameActiveBean>>get(HttpConfig.BASE_URL + HttpConfig.GAME_REDYNAMIC)
                .params("mobile", MyAppLication.getMobile())
                .execute(new JsonCallBack<HttpData<GameActiveBean>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<GameActiveBean>> response) {
                        HttpData<GameActiveBean> httpListData = response.body();
                        if (httpListData.getCode() == 200) {
                            mView.onGetRedynamicSuccess(httpListData.getData());
                        } else {
                            mView.onError(httpListData.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<HttpData<GameActiveBean>> response) {
                        super.onError(response);
                        HttpData httpListData = response.body();
                        if (httpListData != null) {
                            mView.onError(httpListData.getMsg());
                        }
                    }
                });
    }

    /**
     * 首页游戏 兑换界面 数据
     */
    public void getExchangeParameters() {
        OkGo.<HttpData<GameExchangeParametersBean>>get(HttpConfig.BASE_URL + HttpConfig.GAME_EXCHANGEPARAMETERS)
                .params("mobile", MyAppLication.getMobile())
                .execute(new JsonCallBack<HttpData<GameExchangeParametersBean>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<GameExchangeParametersBean>> response) {
                        HttpData<GameExchangeParametersBean> httpListData = response.body();
                        if (httpListData.getCode() == 200) {
                            mView.onGetExchangeParametersSuccess(httpListData.getData());
                        } else {
                            mView.onError(httpListData.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<HttpData<GameExchangeParametersBean>> response) {
                        super.onError(response);
                        HttpData httpListData = response.body();
                        if (httpListData != null) {
                            mView.onError(httpListData.getMsg());
                        }
                    }
                });
    }

    /**
     * 首页游戏 兑换
     *
     * @param eid 兑换参数返回 下标
     * @param num 要兑换的数量
     */
    public void commitExchange(String eid, String num, String password) {
        OkGo.<HttpData<String>>post(HttpConfig.BASE_URL + HttpConfig.GAME_EXCHANGE)
                .params("eid", eid)
                .params("num", num)
                .params("payment_password", MD5Util.getMd5Value(password))
                .execute(new JsonCallBack<HttpData<String>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<String>> response) {
                        HttpData httpListData = response.body();
                        if (httpListData.getCode() == 200) {
                            mView.onCommitExchangeSuccess(httpListData.getMsg());
                        } else {
                            mView.onError(httpListData.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<HttpData<String>> response) {
                        super.onError(response);
                        HttpData httpListData = response.body();
                        if (httpListData != null) {
                            mView.onError(httpListData.getMsg());
                        }
                    }
                });
    }

    /**
     * 首页游戏 孔雀列表
     */
    public void getPeacockList() {
        OkGo.<HttpData<GamePackgaeBean>>get(HttpConfig.BASE_URL + HttpConfig.GAME_PEACOCKLIST)
                .execute(new JsonCallBack<HttpData<GamePackgaeBean>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<GamePackgaeBean>> response) {
                        HttpData<GamePackgaeBean> httpListData = response.body();
                        if (httpListData.getCode() == 200) {
                            mView.onGetPeacockListSuccess(httpListData.getData());
                        } else {
                            mView.onError(httpListData.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<HttpData<GamePackgaeBean>> response) {
                        super.onError(response);
                        HttpData httpListData = response.body();
                        if (httpListData != null) {
                            mView.onError(httpListData.getMsg());
                        }
                    }
                });
    }

    /**
     * 首页游戏 领取蛋
     */
    public void getEggs() {
        OkGo.<HttpData<String>>get(HttpConfig.BASE_URL + HttpConfig.GAME_GETEGGS)
                .params("mobile", MyAppLication.getMobile())
                .execute(new JsonCallBack<HttpData<String>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<String>> response) {
                        HttpData httpListData = response.body();
                        if (httpListData.getCode() == 200) {
                            mView.onGetEggsSuccess(httpListData.getMsg());
                        } else {
                            mView.onError(httpListData.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<HttpData<String>> response) {
                        super.onError(response);
                        HttpData httpListData = response.body();
                        if (httpListData != null) {
                            mView.onError(httpListData.getMsg());
                        }
                    }
                });
    }

    /**
     * 首页游戏 商城列表
     */
    public void getCommunitiesList() {
        OkGo.<HttpData<List<GameCommunitiesListBean>>>get(HttpConfig.BASE_URL + HttpConfig.GAME_COMMUNITIES)
                .params("mobile", MyAppLication.getMobile())
                .execute(new JsonCallBack<HttpData<List<GameCommunitiesListBean>>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<List<GameCommunitiesListBean>>> response) {
                        HttpData<List<GameCommunitiesListBean>> httpListData = response.body();
                        if (httpListData.getCode() == 200) {
                            mView.getCommunitiesListSuccess(httpListData.getData());
                        } else {
                            mView.onError(httpListData.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<HttpData<List<GameCommunitiesListBean>>> response) {
                        super.onError(response);
                        HttpData httpListData = response.body();
                        if (httpListData != null) {
                            mView.onError(httpListData.getMsg());
                        }
                    }
                });
    }


    /**
     * 首页游戏 商城下单
     *
     * @param id               兑换参数返回 下标
     * @param payment_password 资金密码
     */
    public void placeOrder(String id, String payment_password) {
        OkGo.<HttpData<List<GameCommunitiesListBean>>>post(HttpConfig.BASE_URL + HttpConfig.GAME_PLACEORDER)
                .params("mobile", MyAppLication.getMobile())
                .params("id", id)
                .params("payment_password", MD5Util.getMd5Value(payment_password))
                .execute(new JsonCallBack<HttpData<List<GameCommunitiesListBean>>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<List<GameCommunitiesListBean>>> response) {
                        HttpData<List<GameCommunitiesListBean>> httpListData = response.body();
                        if (httpListData.getCode() == 200) {
                            mView.placeOrderSuccess(httpListData.getMsg());
                        } else {
                            mView.onError(httpListData.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<HttpData<List<GameCommunitiesListBean>>> response) {
                        super.onError(response);
                        HttpData httpListData = response.body();
                        if (httpListData != null) {
                            mView.onError(httpListData.getMsg());
                        }
                    }
                });
    }

    /**
     * 首页游戏 公告
     */
    public void getSystemPostsList() {
        OkGo.<HttpData<GameNotificationBean>>get(HttpConfig.BASE_URL + HttpConfig.GAME_SYSTEMPOSTS)
                .params("type", "2")
                .execute(new JsonCallBack<HttpData<GameNotificationBean>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<GameNotificationBean>> response) {
                        HttpData<GameNotificationBean> httpListData = response.body();
                        if (httpListData.getCode() == 200) {
                            mView.getSystemPostsListSuccess(httpListData.getData());
                        } else {
                            mView.onError(httpListData.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<HttpData<GameNotificationBean>> response) {
                        super.onError(response);
                        HttpData httpListData = response.body();
                        if (httpListData != null) {
                            mView.onError(httpListData.getMsg());
                        }
                    }
                });
    }

    /**
     * 首页游戏 领食料界面参数
     */
    public void getTaskList() {
        OkGo.<HttpData<GameGetFoodBean>>get(HttpConfig.BASE_URL + HttpConfig.GAME_TASKLIST)
                .params("type", "2")
                .execute(new JsonCallBack<HttpData<GameGetFoodBean>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<GameGetFoodBean>> response) {
                        HttpData<GameGetFoodBean> httpListData = response.body();
                        if (httpListData.getCode() == 200) {
                            mView.getTaskListSuccess(httpListData.getData());
                        } else {
                            mView.onError(httpListData.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<HttpData<GameGetFoodBean>> response) {
                        super.onError(response);
                        HttpData httpListData = response.body();
                        if (httpListData != null) {
                            mView.onError(httpListData.getMsg());
                        }
                    }
                });
    }

    /**
     * 首页游戏 完成任务后领食料
     */
    public void getTask(String id) {
        OkGo.<HttpData<String>>get(HttpConfig.BASE_URL + HttpConfig.GAME_GETTASK)
                .params("id", id)
                .execute(new JsonCallBack<HttpData<String>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<String>> response) {
                        HttpData httpListData = response.body();
                        if (httpListData.getCode() == 200) {
                            mView.getTaskSuccess(httpListData.getMsg());
                        } else {
                            mView.onError(httpListData.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<HttpData<String>> response) {
                        super.onError(response);
                        HttpData httpListData = response.body();
                        if (httpListData != null) {
                            mView.onError(httpListData.getMsg());
                        }
                    }
                });
    }

}
